package cn.buk.tms.entity.bill;

import cn.buk.common.util.DecimalUtil;
import cn.buk.tms.entity.misc.Customer;
import cn.buk.tms.entity.misc.FlightInfo;
import com.fasterxml.jackson.annotation.JsonFormat;

import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yfdai on 2017-12-6
 * 账单下的增值服务订单内容
 * @author yfdai
 */
@NamedQueries({
        @NamedQuery(
                name = "BillVasOrder.searchMonthly",
                query = "select new cn.buk.tms.dto.ReportVasDto(o.productType, o.productName, " +
                        "b.opDate, o.remark, " +
                        "o.price, o.discount, o.total, o.cost, o.count, o.profit," +
                        "b.op1, b.op2, b.op3, b.customerType, b.id, b.supplier, b.paymentMethod, o.customerId) from BillVasOrder o left join BillInfo b on o.billNum = b.billNum " +
                        " where o.enterpriseId = :enterpriseId and b.opDate >= :beginDate and b.opDate <= :endDate " +
                        " order by b.opDate, o.productName"
        ),
        //以上查询语句，因为散客的customer_id字段为空，会导致散客的结果出不来，需要改进
})
@Entity
@Table(name = "bill_vas_order")
public class BillVasOrder {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private int id;

    @Column(name = "enterprise_id", updatable = false)
    private int enterpriseId;

    /**
     * 账单号
     **/
    @Column(length = 32, nullable = false)
    private String billNum;


    /**
     * 订单号
     */
    @Column(name = "order_no", length = 10)
    private String orderNo;

    /**
     * 服务单的生成日期（用于结算单报表）
     */
    @JsonFormat(pattern = "yyyy-MM-dd",timezone="GMT+8")
    @Temporal(TemporalType.DATE)
    @Column(name = "order_date")
    private Date orderDate;


    /**
     * 商品类型： 保险，门票等
     */
    @Column(name = "product_type", updatable = false)
    private int productType;

    /**
     * 增值服务产品的代码：TieInProduct.productCode
     */
    @Column(name = "product_code")
    private String productCode;

    @Column(name = "product_name")
    private String productName;

    /**
     * 客户类型
     * 0 - 代表散客
     * 1 - 代表企业客户
     */
    @Column(name="customer_type")
    private int customerType;

    /**
     * 客户在系统中的id: 在t102中对应的ID
     */
    @ManyToOne
    @JoinColumn(name="customer_id")
    private Customer customer;

    @Column(name = "customer_id", insertable = false, updatable = false)
    private Integer customerId;

    /**
     * 成本中心
     */
    @Column(name = "cost_center", length = 50)
    private String costCenter;

    /**
     * 标价
     */
    private double price;

    /**
     * 折扣金额
     */
    private double discount;

    /**
     * 成本
     */
    private double cost;

    /**
     * 数量
     */
    private int count;

    /**
     * 利润 = count * (price - discount- cost)
     */
    private double profit;

    private Double total;

    /**
     * 该账单客户的付款方式
     * 1-现金；
     * 2-信用卡；
     * 4-支票；
     * 8-记账
     */
    @Column(name = "pay_type")
    private int payType;

    /**
     * 付款状态(只有付款类型非记账方式时才有用）
     * 0 or null 未支付
     * 1 已支付
     */
    @Column(name = "pay_status")
    private Integer payStatus;

    /**
     * 是否已付款(财务销账）
     */
    @Column(name = "be_paid", length = 1)
    private String bePaid;

    /**
     * 服务订单中对应的人名（如果有），多个人名用分号（；）隔开
     */
    @Column(name = "psg_name", length = 200)
    private String passengerName;

    /**
     * 服务订单中对应的人名数量
     */
    @Column(name = "psg_count")
    private Integer psgCount;

    /**
     * 服务订单中对应的航班信息（如果有），方便做报表的时候显示
     */
    @Column(name = "route_detail", length = 200)
    private String routeDetail;


    private String remark;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_time", insertable = false, updatable = false)
    private Date createTime;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_update", insertable = false)
    private Date lastUpdate;

    @Transient
    private List<FlightInfo> flights;

    @Transient
    private List<BillVasOrderPassenger> passengers;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getProductCode() {
        return productCode;
    }

    public void setProductCode(String productCode) {
        this.productCode = productCode;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public Date getLastUpdate() {
        return lastUpdate;
    }

    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    public double getProfit() {
        return DecimalUtil.keepScale2(profit);
    }

    public void setProfit(double profit) {
        this.profit = profit;
    }

    public int getCustomerType() {
        return customerType;
    }

    public void setCustomerType(Integer customerType) {
        this.customerType = customerType;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    public int getPayType() {
        return payType;
    }

    public void setPayType(int payType) {
        this.payType = payType;
    }

    public int getPayStatus() {
        return payStatus == null ? 0 : payStatus;
    }

    public void setPayStatus(Integer payStatus) {
        this.payStatus = payStatus;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public double getTotal() {
        return total == null ? 0 : total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public int getProductType() {
        return productType;
    }

    public void setProductType(int productType) {
        this.productType = productType;
    }

    public String getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }

    public String getCostCenter() {
        return costCenter;
    }

    public void setCostCenter(String costCenter) {
        this.costCenter = costCenter;
    }

    public String getBillNum() {
        return billNum;
    }

    public void setBillNum(String billNum) {
        this.billNum = billNum;
    }

    public Integer getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Integer customerId) {
        this.customerId = customerId;
    }

    public String getBePaid() {
        return bePaid;
    }

    public void setBePaid(String bePaid) {
        this.bePaid = bePaid;
    }

    public Date getOrderDate() {
        return orderDate;
    }

    public void setOrderDate(Date orderDate) {
        this.orderDate = orderDate;
    }

    public String getPassengerName() {
        return passengerName;
    }

    public void setPassengerName(String passengerName) {
        this.passengerName = passengerName;
    }

    public String getRouteDetail() {
        return routeDetail;
    }

    public void setRouteDetail(String routeDetail) {
        this.routeDetail = routeDetail;
    }

    public List<FlightInfo> getFlights() {
        if (flights == null) {
          flights = new ArrayList<>();
        }
        return flights;
    }

    public void setFlights(List<FlightInfo> flights) {
        this.flights = flights;
    }

    public int getPsgCount() {
        return psgCount == null ? -1: psgCount;
    }

    public void setPsgCount(Integer psgCount) {
        this.psgCount = psgCount;
    }

    public List<BillVasOrderPassenger> getPassengers() {
        if (passengers == null) {
            passengers = new ArrayList<>();
        }
        return passengers;
    }

    public void setPassengers(List<BillVasOrderPassenger> passengers) {
        this.passengers = passengers;
    }
}
